################
# TABLE 2, 3, 4
################
library(stargazer)
library(xtable)
library(data.table)
library(foreign)
load("model_data.RData")

# TABLE 2 AND 3

mod1_simp <- lm(bias ~ black + iso3c, data = mod_dat)

mod1_regions <- lm(bias ~ test_regions + 
  iso3c, 
  data = mod_dat)

stargazer::stargazer(mod1_simp, mod1_regions,
  omit = c("iso3c", "factor"), style = "apsr",
  covariate.labels = c("Black", "Arab", "East Asian",
  "Latin American", "Non-OECD, White", "OECD, White",
  "South Asian", "Southeast Asian",
  "West Asian", "(Intercept)"),
  ci = TRUE,
  digits = 2, align = TRUE,
  add.lines = list(c("Country FE", "\\checkmark", "\\checkmark")),
  dep.var.labels = "Deviation",
  ci.level = 0.95,
  star.cutoffs = c(0.05, 0.01, 0.001))

hier_tab <- data.table(estimate = coef(mod1_regions)[2:9], `Racial Category` =
  c("Arab", "East Asian", "Latin American", "Non-OECD, White",
    "OECD, White", "South Asian", "Southeast Asian", "West Asian"))
xtable::xtable(hier_tab[order(-estimate)])

# TABLE 4

gen_dist <- foreign::read.dta("genetic_distance.dta")
setDT(gen_dist)
gen_dist$origin <-
  countrycode::countrycode(gen_dist$country_1, "country.name", "iso3c")
gen_dist$dest <-
  countrycode::countrycode(gen_dist$country_2, "country.name", "iso3c")
gen_dist <- gen_dist[, .(fst_distance_weighted, origin, dest)]
setDT(gen_dist)
bias_dat <- merge(bias_dat, gen_dist, by = c("origin", "dest"))

bias_dat$gen_dist <- arm::rescale(bias_dat$fst_distance_weighted)



mod1 <- lm(bias ~ gen_dist,
  data = bias_dat)
mod2 <- lm(bias ~ gen_dist +
  origin +
  dest,
  data = bias_dat)

mod3 <- lm(bias ~ gen_dist +
  origin +
  dest +
  origin_pd +
  dest_pd,
  data = bias_dat)

stargazer::stargazer(mod1, mod2, mod3, style = "apsr",
  covariate.labels = c("Allele Distance"), ci = TRUE,
  digits = 2, omit = c("dest", "origin", "year"), align = TRUE,
  add.lines = list(c("Origin FE", "\\textnormal{X}", "\\checkmark", "\\checkmark"),
  c("Destination FE", "\\textnormal{X}", "\\checkmark", "\\checkmark"),
  c("Origin-Period FE", "\\textnormal{X}", "\\textnormal{X}", "\\checkmark"),
  c("Destination-Period FE", "\\textnormal{X}", "\\textnormal{X}", "\\checkmark")),
  dep.var.labels = "Deviation",
  ci.level = 0.95,
  star.cutoffs = c(0.05, 0.01, 0.001))
